package listTest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ComparatorList {
    public static void main(String[] args) {
        listTest();
    }

    public static void listTest() {
        ArrayList<User> list = new ArrayList<>();
        list.add(new User(16));
        list.add(new User(17));
        list.add(new User(18));
        list.add(new User(19));

        //升序排列
        //list.sort(Comparator.comparing(User::getAge));
        //加上reversed()变成降序排列
        list.sort(Comparator.comparing(User::getAge).reversed());
        System.out.println(list);


        /** 降序排列
         Collections.sort(list, new Comparator<User>() {
        @Override public int compare(User u1, User u2) {
        return u2.getAge() - u1.getAge();
        }
        });*/

        //获取最大值
        User max = Collections.max(list, Comparator.comparingInt(User::getAge));
        //获取list的最小值
        User min = Collections.min(list, Comparator.comparingInt(User::getAge));
        System.out.println(max);
        System.out.println("-----------------------");
        System.out.println(min);
        System.out.println(list.get(2));

        for (int i = 0; i < list.size(); i++) {
            User user1 = list.get(i);
            System.out.println(user1.getAge());
        }


    }
}
